import { ref, onUnmounted } from 'vue';
 const reConnectTimeout = ref(null);
export function useWebSocket() {
  const socketOpen = ref(false);
  const reSocketOpen = ref(false);
  const setIntervalName = ref(null);
 

  const connectWebSocket = () => {
    // let baseUrl = 'http://172.18.3.8:8090/api';
    let baseUrl = 'https://jtmckj.cn:4090/api';
    let url;

    switch (baseUrl) {
      case "http://172.18.3.8:8090/api":
        url = 'ws://172.18.3.8:9527/websocket';
        break;
      case "https://jtmckj.cn:4090/api":
        url = 'wss://jtmckj.cn:9995/websocket';
        break;
      case "https://jtmckj.com/api":
        url = 'wss://jtmckj.com:9996/websocket';
        break;
      default:
        console.error('Invalid baseUrl');
        return;
    }

    uni.connectSocket({
      url,
      success: () => {
        console.log('WebSocket连接成功');
      },
      fail: (res) => {
        console.log('WebSocket连接失败:', res);
        reConnect();
      }
    });

    uni.onSocketOpen(() => {
      console.log('WebSocket连接已打开');
      socketOpen.value = true;
	  const globalData = uni.getStorageSync('globalData')
	  console.log(globalData,'globalData接收的参数')
	  const initialMessage = {
		   msgType: "bottle",
		   subMsgType:"heartbeat",
		   msgPlatform:3,
				msgContent: {
					 orgCode: globalData.orgCode,
					 orgName:globalData.orgName,
					 memberCode:globalData.code,
					 memberName:globalData.name
				},
	  };

      sendSocketMessage(initialMessage);

      setIntervalName.value = setInterval(() => { 
        sendSocketMessage(initialMessage);
      }, 10000);
      // }, 2000); 
    });

    uni.onSocketError((res) => {
      console.log('WebSocket连接打开失败:', res);
      reConnect();
    });

    uni.onSocketClose(() => {
      console.log('WebSocket连接已关闭');
	  // reSocketOpen.value = false
      reConnect();
    });

  //   uni.onSocketMessage((message) => {
  //     console.log('收到WebSocket消息:', message);
  //     try {
  //       const data = JSON.parse(message.data);

  //       handleMessage(data);
		
  //     } catch (error) {
  //       console.error('处理WebSocket消息时出错:', error);
  //     }
  //   });
  };

  const sendSocketMessage = (message) => {
    uni.sendSocketMessage({
      data: JSON.stringify(message),
      success: (e) => {
      },
      fail: (err) => {
        console.log(err);
        reConnect();
      }
    });
  };

  const reConnect = () => {
    // if (reSocketOpen.value) return;
    // clearTimeout(reConnectTimeout.value);
   //  reConnectTimeout.value = setTimeout(() => {
   //    reSocketOpen.value = true;
	  // console.log('重连看看22222222222222')
   //    connectWebSocket();
   //  }, 15000);
   
	 setTimeout(()=>{
		 console.log('重连')
		connectWebSocket();
	    },10000)
	//     },2000)
	
  };

  const handleMessage = (data) => {
    
  };

  onUnmounted(() => {
    clearInterval(setIntervalName.value);
    // clearTimeout(reConnectTimeout.value);
  });

  return {
    connectWebSocket,
    sendSocketMessage,
	socketOpen
  };
}
